<?php


namespace App\Services;

use App\Models\Car;
use App\Models\CarBrand;
use App\Models\DriverCar;
use App\Models\Drivers;
use App\Models\ServiceType;
use App\Models\UploadFile;
use App\Models\User;
use Illuminate\Support\Facades\Cache;

class CarService extends BaseService
{
    protected $model;

    function __construct()
    {
        $this->model = new Car();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * 司机车辆管理
     */
    public function getList(User $user)
    {
        $driver_id = $user->driver()->first() ? $user->driver()->first()->id : 0;
        $taxi_status = $user->driver()->first() ? $user->driver()->first()->taxi_status : '-2';
        $status = $user->driver()->first() ? $user->driver()->first()->status : -2;
        $car_id = DriverCar::query()->where('driver_id', $driver_id)->get('car_id');
        if ($status == '2' && $taxi_status != '2') {
            $name = '优米';
        } elseif ($taxi_status == '2' && $status != '2') {
            $name = '出租车';
        } else {
            $name = '';
        }
        if ($name == '') {
            $ss = ServiceType::query()->where('status', 1)->get('id');
        } else {
            $ss = ServiceType::query()->where('name', 'like', '%' . $name . '%')->where('status', 1)->get('id');
        }
        $car = $this->model
            ->whereIn('id', $car_id)
            ->whereIn('type', $ss)
            ->select(['id', 'outward_dr_id as driver_id', 'number', 'type', 'restriction', 'ownership', 'colour', 'status', 'company_id', 'brand_id', 'license_driving', 'outward_status', 'type'])
            ->with(['brand:id,name', 'service_type:id,name'])
            ->get()
            ->toArray();
        foreach ($car as $item => $value) {
            if (!is_array($value['license_driving'])) {
                $value['license_driving'] = json_decode($value['license_driving'], true);
            }
            if (!empty($value['license_driving'])) {
                $car[$item]['license_driving'] = UploadFile::query()->where('id', end($value['license_driving']))->select(['file_url', 'file_name'])->first();
            } else {
                $car[$item]['license_driving'] = new \stdClass;
            }
        }
        return $car;
    }

    /**
     * @param int $car_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null|object
     * @throws \App\Exceptions\BobException
     * 状态查看
     */
    public function car_status(int $car_id)
    {
        $info = Car::query()->where('id', $car_id)->select(['id', 'outward_status', 'outward_dr_id'])->first();
        if (!$info) return_bobc('车辆不存在');
        return $info;
    }

    /**
     * @param int $car_id
     * @param $type
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null|object
     * @throws \App\Exceptions\BobException
     * 修改状态
     */
    public function car_edit_statussss(User $user, int $car_id, string $type)
    {

        $driver_id = $user->driver()->first()->id;
        if (!$driver_id) return_bobc('司机不存在');
        $info = Car::query()->where('id', $car_id)->update(['outward_status' => $type, 'outward_dr_id' => $driver_id]);
        if ($info) {
            return 1;
        }
        return 0;
    }


    /**
     * 车辆品牌
     */
    public function car_brand()
    {
        return CarBrand::query()->get()->toArray();
    }

    /**
     * 编辑车辆信息
     */
    public function car_edits(int $car_id)
    {
        $drivers = DriverCar::with('drivers')->where('car_id', $car_id)->get()->toArray();
        $data = [];
        foreach ($drivers as $key => $val) {
            $data[$key]['car_id'] = $drivers[$key]['car_id'];
            $data[$key]['host'] = $drivers[$key]['host'];
            $data[$key]['id'] = $drivers[$key]['drivers']['id'];
            $data[$key]['real_name'] = $drivers[$key]['drivers']['real_name'];
            $data[$key]['phone'] = ycPhone($drivers[$key]['drivers']['phone']);
        }
        return $data;
    }

    /**
     * @param string $phone
     * 查询司机是否存在
     */
    public function drivers(string $phone)
    {
        if ($drivers = Drivers::query()->where('phone', $phone)->first(['id', 'phone', 'real_name'])) {
            return $drivers;
        }
        return false;
    }

    /**
     * 转让主司机 ,删除司机
     */
    public function drivers_car_int(int $car_id, int $driver_id, $type)
    {
        $model = new DriverCar();
        //转让司机
        if ($type == 1) {
            $drivers_car = $model->where(['car_id' => $car_id, 'driver_id' => $driver_id])->update(['host' => 1]);
            if ($drivers_car) {
                $drivers_car = $model->where(['car_id' => $car_id])->whereNotIn('driver_id', $driver_id)->update(['host' => 0]);
            }
        } else {
            $drivers_car = $model->where(['car_id' => $car_id, 'driver_id' => $driver_id])->delete();
        }
        return $drivers_car ? $drivers_car : false;
    }
}
